import { DragItem, ChapterDragItem, CharacterDragItem, FolderDragItem } from '@/types/drag-drop'

export interface FormattedContent {
  type: string;
  title: string;
  summary: string;
  details: Record<string, any>;
}

export const formatDragContent = (items: DragItem[]): FormattedContent[] => {
  return items.map(item => {
    switch (item.type) {
      case 'chapter':
        return formatChapterContent(item as ChapterDragItem)
      case 'character':
        return formatCharacterContent(item as CharacterDragItem)
      case 'folder':
        return formatFolderContent(item as FolderDragItem)
      default:
        return {
          type: item.type,
          title: item.name,
          summary: '未知类型的内容',
          details: item.data || {}
        }
    }
  })
}

const formatChapterContent = (item: ChapterDragItem): FormattedContent => {
  const content = item.data.content
  const summary = content.length > 100 
    ? content.substring(0, 100) + '...' 
    : content
  
  return {
    type: 'chapter',
    title: item.data.title,
    summary,
    details: {
      projectId: item.data.projectId,
      order: item.data.order,
      fullContent: content
    }
  }
}

const formatCharacterContent = (item: CharacterDragItem): FormattedContent => {
  const traits = item.data.traits || []
  const traitSummary = traits
    .map(t => `${t.name}: ${t.value}`)
    .join('; ')
  
  return {
    type: 'character',
    title: item.data.name,
    summary: `${item.data.description || '无描述'}\n${traitSummary}`,
    details: {
      description: item.data.description,
      traits: item.data.traits,
      background: item.data.background,
      avatar: item.data.avatar
    }
  }
}

const formatFolderContent = (item: FolderDragItem): FormattedContent => {
  const items = item.data.items || []
  const itemCount = items.length
  
  return {
    type: 'folder',
    title: item.name,
    summary: `包含${itemCount}个项目`,
    details: {
      items: formatDragContent(items)
    }
  }
}

export const createAIPrompt = (contents: FormattedContent[]): string => {
  const prompts = contents.map(content => {
    switch (content.type) {
      case 'chapter':
        return `章节《${content.title}》的内容：\n${content.summary}`
      case 'character':
        return `角色${content.title}的信息：\n${content.summary}`
      case 'folder':
        return `文件夹《${content.title}》包含以下内容：\n${content.details.items.map((item: FormattedContent) => 
          `- ${item.title}（${item.type}）`
        ).join('\n')}`
      default:
        return `内容：${content.title}\n${content.summary}`
    }
  })
  
  return `以下是我提供的内容，请基于这些内容提供帮助：\n\n${prompts.join('\n\n')}`
} 